package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SetArea;
import com.ruoyi.system.domain.stores.EquipmentLedger;
import com.ruoyi.system.mapper.EquipmentLedgerMapper;
import com.ruoyi.system.service.IEquipmentLedgerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author GE-LV5
 * @create 2023/2/17 14:15
 * ========设备台账==============
 */
@Service
@Slf4j
public class EquipmentLedgerServiceImpl extends ServiceImpl<EquipmentLedgerMapper, EquipmentLedger> implements IEquipmentLedgerService {

    @Autowired
    EquipmentLedgerMapper equipmentLedgerMapper;

    /**
     * 查询所有
     * @param dept 设备台账
     * @return
     */
    @Override
    public List<EquipmentLedger> selectTransferSlipList(EquipmentLedger dept) {
        return   equipmentLedgerMapper.selectEquipmentLedgerList(dept);
    }

    /**
     * 根据id去删除
     * @param id
     * @return
     */
    @Override
    public int deleteEquipmentLedgerById(Long id) {
        return equipmentLedgerMapper.deleteEquipmentLedgerById(id);
    }
    /**
     * 批量删除区域
     *
     * @param ids 需要删除的区域主键
     * @return 结果
     */
    @Override
    public int deleteSetAreaByIds(List<Long> ids) {
        return equipmentLedgerMapper.deleteSetStepByIds(ids);
    }



    /**
     * 新增设备台账
     * @param ordersCustomer 设备台账
     * @return
     */
    @Override
    @Transactional
    public int insertEquipmentLedger(EquipmentLedger ordersCustomer) {
        if (StringUtils.isNull(ordersCustomer))
        {
            throw new CustomException("添加失败！");
        }
        ordersCustomer.setCreateTime(DateUtils.getNowDate());
        ordersCustomer.setCreateBy(SecurityUtils.getUsername());
        ordersCustomer.setStatus(UserConstants.NORMAL);
        return equipmentLedgerMapper.insertEquipmentLedger(ordersCustomer);
    }

    /**
     * 修改设备台账
     * @param ordersManual 设备台账
     * @return
     */
    @Override
    @Transactional
    public int updateOrdersManual(EquipmentLedger ordersManual) {
        ordersManual.setUpdateTime(DateUtils.getNowDate());
        ordersManual.setUpdateBy(SecurityUtils.getUsername());
        return equipmentLedgerMapper.updateEquipmentLedger(ordersManual);
    }

    @Override
    public EquipmentLedger equipmentLedgerById(Long id) {
        return equipmentLedgerMapper.equipmentLedgerById(id);
    }

    @Override
    public String importArea(List<EquipmentLedger> areaList, boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(areaList) || areaList.size() == 0)
        {
            throw new CustomException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        //String password = configService.selectConfigByKey("sys.user.initPassword");
        for (EquipmentLedger setArea : areaList)
        {
            try
            {
                // 验证是否存在这个区域
                EquipmentLedger equipmentLedgers = equipmentLedgerMapper.selectEquipmentLedgerCode(setArea.getEquipmentCode());
                System.out.println(equipmentLedgers);
                if (StringUtils.isNull(equipmentLedgers))
                {
                    setArea.setCreateBy(operName);
                    this.insertEquipmentLedger(setArea);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备台账 " + setArea.getEquipmentName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    setArea.setUpdateBy(operName);
                    this.updateOrdersManual(setArea);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、设备台账 " + setArea.getEquipmentName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、设备台账 " + setArea.getEquipmentName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、设备台账 " + setArea.getEquipmentName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public EquipmentLedger selectEquipmentLedgerCode(String equipmentCode) {
        return equipmentLedgerMapper.selectEquipmentLedgerCode(equipmentCode);
    }

    @Override
    public List<EquipmentLedger> selectEquipmentList(EquipmentLedger equipmentLedger) {
        return equipmentLedgerMapper.selectEquipmentList(equipmentLedger);
    }
}


